<?php

namespace app\plug\admin\vovoqi_novel;

use app\_interface\ResourceInterface;
use app\common\entity\ResponseData;
use Sffi\ElementPlus\ElForm;
use Sffi\ElementPlus\ElList;

class Novel extends \app\admin\controller\AdminBase implements ResourceInterface{
    public function list(){
        #可从Request中获取当前用户
        $user = $this->request->user;
        $page = input('page', 1);
        $size = input('size', 10);
        #分页固定参数为：page和size，page控制页数，size控制每页数据量
        $novelModel = \app\plug\model\vovoqi_novel\Novel::page($page, $size);
        $count = $novelModel->count();
        $list = $novelModel->select();
        #返回参数说明，第一个参数是列表数据，第二个参数是个数组，数组包含一下属性：sum数据总条数，page当前页数，size当前每页数据量
        return ResponseData::success($list, ['sum' => $count, 'page'=>$page, 'size'=>$size]);
    }

    public function detail(){
        #数据详情接口，配合getFormColumns接口回显数据。
        return ResponseData::success([
            'title'=>'我道异界'
        ]);
    }

    public function add(){
        #数据新增接口
        $novel = \app\plug\model\vovoqi_novel\Novel::create(input('data'));
        return ResponseData::success($novel);
    }

    public function update(){
        #数据更新接口
        $id = input('id');
        $data = input();
        $novel = \app\plug\model\vovoqi_novel\Novel::find($id);
        $novel->save($data);
        return ResponseData::success($novel);
    }

    public function delete($id){
        #数据删除接口
        return ResponseData::success('');
    }

    public function getListColumns(){
        #该接口必须实现：定义展示的列表数据
        $list = new ElList();

        $list->addColumn(new ElList\Column('小说标题', 'title', 'title', 100));

        return ResponseData::success($list->__toArray());
    }

    public function getFormColumns(){
        #该接口必须实现：定义数据编辑页面
        $form = new ElForm();
        $form->addColumn(new ElForm\ElInput('小说标题', 'title', '请输入小说标题'));

        return ResponseData::success($form->__toArray());
    }
}